package com.fjwt.gz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.LiteHomepageDTO;
import com.fjwt.gz.db.entity.LiteHomepageEntity;
import com.fjwt.gz.db.vo.LiteHomepageVO;
import com.fjwt.gz.service.mapper.LiteHomepageMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 小程序首页配置表 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class LiteHomepageService extends ServiceImpl<LiteHomepageMapper, LiteHomepageEntity> {

    @Resource
    private LiteHomepageMapper liteHomepageMapper;

    public IPage<LiteHomepageEntity> selectLiteHomepageList(IPage page, Map map) {
        return liteHomepageMapper.selectLiteHomepageList(page,map);
    }

    /**
     * 首页配置新增
     */
    public void insert(LiteHomepageDTO dto,Long sysUserId) {
        LiteHomepageEntity entity = BeanUtil.copyProperties(dto, LiteHomepageEntity.class);
        entity.setCreatedUid(sysUserId);
        entity.setCreatedAt(new Date());
        boolean flag = save(entity);
        if (!flag) {
            log.error("新增失败！");
        }
    }

    /**
     * 首页配置修改
     */
    public void updateLiteHomepage(LiteHomepageDTO dto,Long sysUserId) {
        LiteHomepageEntity entity = BeanUtil.copyProperties(dto, LiteHomepageEntity.class);
        entity.setCreatedUid(sysUserId);
        entity.setUpdateAt(new Date());
        boolean flag = updateById(entity);
        if (!flag) {
            log.error("修改失败！");
        }
    }

    public LiteHomepageVO getInfoById(LiteHomepageDTO dto) {
        if (Objects.isNull(dto.getHomepageId())) {
            throw new RuntimeException("请求参数缺失！");
        }
        LiteHomepageEntity entity = getById(dto.getHomepageId());
        LiteHomepageVO vo = BeanUtil.copyProperties(entity, LiteHomepageVO.class);
        return vo;
    }
}
